package com.bz.xtcx.manager.service.impl;

import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.entity.LibCodeSequence;
import com.bz.xtcx.manager.mapper.LibCodeSequenceMapper;
import com.bz.xtcx.manager.service.IResCodeCreater;
import com.bz.xtcx.manager.utils.WzDateUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ResCodeCreater extends BaseService implements IResCodeCreater {
    @Value("${wzsetting.res-prefixs}")
    private String resPrefixs;

    @Autowired
    private LibCodeSequenceMapper libCodeSequenceMapper;

    @Override
    public String makeResCode(String codePrefix) throws XtcxException {
        if (WzStrUtil.isBlank(codePrefix)) {
            throw new XtcxException("未获得资源前缀");
        }
        if (WzStrUtil.isNotBlank(resPrefixs)) {
            if (-1 == resPrefixs.indexOf(codePrefix.trim().toUpperCase())) {
                throw new XtcxException("资源前缀不正确，仅支持ZJ（专家）、QY（企业）,GXYS（高校院所）,FWJG（服务机构）,XQ（需求）,CG（成果）,DJ（对接）");
            }
        }
        int codeSeq = libCodeSequenceMapper.findCodeByPrefix(codePrefix.trim());
        LibCodeSequence lcs = libCodeSequenceMapper.findByPrefix(codePrefix.trim());
        String pfv = codePrefix.trim();
        if (null != lcs && WzStrUtil.isNotBlank(lcs.getPrefixValue())) {
            pfv = lcs.getPrefixValue().trim();
        }
        return pfv + WzDateUtil.dt2Str(new Date(), WzDateUtil.DT_FMT_3) + WzStrUtil.defaultString(WzStrUtil.leftPad(String.valueOf(codeSeq), 5, "0"), "99999");
    }
}
